package com.yhp.web;

import com.yhp.bo.UsersBO;
import com.yhp.pojo.Users;
import com.yhp.service.UserService;
import com.yhp.utils.CookieUtils;
import com.yhp.utils.JSONResult;
import com.yhp.utils.JsonUtils;
import com.yhp.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/passport")
//@ApiIgnore  用于忽略swagger扫描类的注解
@Api(value = "注册登录",tags = {"注册登录tags"})
public class PassPortController {

    //log4j
    final  static Logger logger= LoggerFactory.getLogger(PassPortController.class);

    @Resource
    private UserService userService;

    //退出方法
    @PostMapping("/logout")
    //注意httpMethod的值大写  value-接口说明，notes-说明，httpMethod-请求方式
    @ApiOperation(value = "用户退出",notes = "用户退出",httpMethod = "POST")
    public JSONResult logout(String userId,HttpServletRequest request,HttpServletResponse response){

            //清除用户相关的cookie
        CookieUtils.deleteCookie(request,response,"user");
        //注意:用户退出登录，需要清空购物车
        //分布式会话中需要清除用户数据
        return  JSONResult.ok();
    }




    //注册
    @PostMapping("/reg")
    //注意httpMethod的值大写  value-接口说明，notes-说明，httpMethod-请求方式
    @ApiOperation(value = "用户注册",notes = "账户注册功能",httpMethod = "POST")
    public JSONResult reg(@RequestBody UsersBO usersBO,HttpServletRequest request,HttpServletResponse response){
        logger.info("info信息---------------");
        String userName = usersBO.getUserName();
        String passWord = usersBO.getPassWord();
        String repeatPass = usersBO.getRepeatPass();
        //1.用户名密码非空验证
        if(StringUtils.isBlank(userName)||
                StringUtils.isBlank(passWord)
                ||StringUtils.isBlank(repeatPass)){
            return JSONResult.errorMsg("用户名或密码不能为空");

        }
        //2.查询用户名是否存在
        boolean b = userService.queryUserName(userName);
        if(b){
            return JSONResult.errorMsg("用户名已存在");
        }
        //3.密码长度验证
        if(passWord.length()<6){
            return JSONResult.errorMsg("密码长度不能少于6位");
        }
        //4.判断两次密码是否一致
        if(!passWord.equals(repeatPass)){
            return JSONResult.errorMsg("两次密码不一致");
        }
        //5.实现注册
        Users users = userService.regUsers(usersBO);
        //为防止重要数据泄露
        users=setNullProperties(users);
        //isEncode  是否加密
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(users),true);


        return  JSONResult.ok(users);
    }


    //用户名验证
    @GetMapping("/usernameisexist")
    //注意httpMethod的值大写  value-接口说明，notes-说明，httpMethod-请求方式
    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在notes",httpMethod = "GET")
    public JSONResult usernameIsExist(String username){

        //判断用户名不能为空
        if(StringUtils.isBlank(username)){
            return JSONResult.errorMsg("用户名不能为null");
        }
        //查询注册的用户名是否存在
        boolean b = userService.queryUserName(username);
        if(b){
            return JSONResult.errorMsg("用户名已存在");
        }
        //请求成功，用户名不重复
        return  JSONResult.ok();
    }


    //登录
    @PostMapping("/login")
    //注意httpMethod的值大写  value-接口说明，notes-说明，httpMethod-请求方式
    @ApiOperation(value = "用户登录",notes = "账户登录功能",httpMethod = "POST")
    public JSONResult login(@RequestBody UsersBO usersBO, HttpServletRequest request, HttpServletResponse response){

        String userName = usersBO.getUserName();
        String passWord = usersBO.getPassWord();
        //1.用户名密码非空验证
        if(StringUtils.isBlank(userName)||
                StringUtils.isBlank(passWord)){
            return JSONResult.errorMsg("用户名或密码不能为空");

        }
        Users users = null;
        try {
            //2.实现注册
            users = userService.login(userName,
                    MD5Utils.getMD5Str(passWord));
            if(users==null){
                return JSONResult.errorMsg("用户登录失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //为防止重要数据泄露
        users=setNullProperties(users);
        //isEncode  是否加密
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(users),true);

        //TODO 生成用户token,存入redis中
        //TODO 同步购物车数据



        return  JSONResult.ok(users);
    }

    public Users setNullProperties(Users users){
        users.setPassword(null);
        users.setRealname(null);
        users.setEmail(null);
        users.setCreatedTime(null);
        users.setUpdatedTime(null);
        users.setBirthday(null);
        return users;
    }

}
